<?php

namespace app {

    use C\ctr;
    use C\lib\Db;
    use C\lib\Rsa;
    use C\lib\Session;

    class main extends ctr {

        // --- 获取最新的 RSA 公钥的 Key 以及检查更新 ---
        public function init() {

            if(isset($_POST['app'])) {
                if(isset($_POST['platform'])) {
                    if(isset($_POST['verRsa'])) {
                        $verApp = '';
                        $appInfo = '';
                        $appUrl = '';
                        $appMust = '';
                        if($_POST['app'] == APP_CROWD) {
                            if($_POST['platform'] == 'iOS') {
                                $verApp = APP_CROWD_IOS_VER;
                                $appInfo = APP_CROWD_IOS_INFO;
                                $appUrl = APP_CROWD_IOS_URL;
                                $appMust = APP_CROWD_IOS_MUST;
                            } else {
                                $verApp = APP_CROWD_VER;
                                $appInfo = APP_CROWD_INFO;
                                $appUrl = APP_CROWD_URL;
                                $appMust = APP_CROWD_MUST;
                            }
                        } else if($_POST['app'] == APP_WORK) {
                            if($_POST['platform'] == 'iOS') {
                                $verApp = APP_WORK_IOS_VER;
                                $appInfo = APP_WORK_IOS_INFO;
                                $appUrl = APP_WORK_IOS_URL;
                            } else {
                                $verApp = APP_WORK_VER;
                                $appInfo = APP_WORK_INFO;
                                $appUrl = APP_WORK_URL;
                            }
                        }
                        $json = ['verRsa' => RSA_VERSION, 'verApp' => $verApp];
                        if(RSA_VERSION != $_POST['verRsa']) $json['keyRsa'] = file_get_contents(RSA_PUBLIC_KEY . '.' . RSA_VERSION);
                        if(version_compare($verApp, $_POST['verApp'], '>')) {
                            $json['appInfo'] = $appInfo;
                            $json['appUrl'] = $appUrl;
                            $json['appMust'] = $appMust;
                        }

                        $this->writeJson(1, $json);
                    } else
                        $this->writeJson(0, 'verRsa传递失败');
                } else
                    $this->writeJson(0, 'platform传递失败');
            } else
                $this->writeJson(0, 'app传递失败');
        }

        // --- 创建 Aes 点对点通讯秘钥 ---
        public function createAesKey() {

            if(isset($_POST['verRsa']) && is_numeric($_POST['verRsa']) && is_file(RSA_PRIVATE_KEY.'.'.$_POST['verRsa'])) {
                if(isset($_POST['rsa'])) {
                    if ($data = Rsa::decrypt($_POST['rsa'], '@' . RSA_PRIVATE_KEY . '.' . $_POST['verRsa'])) {
                        if ($data = json_decode($data, true)) {
                            if (isset($data['time'])) {
                                if ($data['time'] > $_SERVER['REQUEST_TIME'] - 600) {
                                    if (strlen($data['key']) == 16) {
                                        Db::connect();
                                        Session::start();
                                        $_SESSION['aes_key'] = $data['key'];
                                        $this->writeAesJson(1, ['msg' => '创建成功']);
                                    } else
                                        $this->writeJson(0, 'Aes 密钥必须等于 16 位');
                                } else
                                    $this->writeJson(0, '请求超时');
                            } else
                                $this->writeJson(0, 'Aes 内容缺少 time 时间戳参数');
                        } else
                            $this->writeJson(0, 'JSON 识别失败');
                    } else
                        $this->writeJson(0, 'RSA 解密失败');
                } else
                    $this->writeJson(0, '请提交 RSA 参数');
            } else
                $this->writeJson(0, '当前公钥已经失效');

        }

    }

}

